package com.chengyu.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengyu.core.dao.ReceiptUserDao;
import com.chengyu.core.dao.ReceiptUserDao;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.exception.CommonException;
import com.chengyu.core.model.ReceiptUser;
import com.chengyu.core.model.ReceiptUser;
import com.chengyu.core.model.ReceiptUserOrder;
import com.chengyu.core.request.PageParamRequest;
import com.chengyu.core.service.ReceiptUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户发票信息 服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-01-17
 */
@Service
public class ReceiptUserServiceImpl extends ServiceImpl<ReceiptUserDao, ReceiptUser> implements ReceiptUserService {

    @Autowired
    private ReceiptUserDao dao;


    /**
     * 获取列表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页参数
     * @return PageInfo
     */
    @Override
    public PageInfo<ReceiptUser> getList(ReceiptUser request, PageParamRequest pageParamRequest) {
        Page<ReceiptUser> ReceiptUserPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());



        LambdaQueryWrapper<ReceiptUser> lqw = Wrappers.lambdaQuery();


        if (ObjectUtil.isNotNull(request.getUid())) {
            lqw.eq(ReceiptUser::getUid,request.getUid());
        }



            lqw.eq(ReceiptUser::getIsDel,0);


//        if (ObjectUtil.isNotNull(request.getMemberId())) {
//            lqw.eq(ReceiptUser::getMemberId, request.getMemberId());
//        }
//
//        if (ObjectUtil.isNotNull(request.getType())) {
//            lqw.eq(ReceiptUser::getType, request.getType());
//        }
//        if (StrUtil.isNotBlank(request.getTitle())) {
//            String title = URLUtil.decode(request.getTitle());
//            lqw.like(ReceiptUser::getTitle, title);
//        }
//        if (StrUtil.isNotBlank(request.getAuthor())) {
//            lqw.eq(ReceiptUser::getAuthor, URLUtil.decode(request.getAuthor()));
//        }
//        lqw.eq(ReceiptUser::getIsDel, false);
//        lqw.orderByDesc(ReceiptUser::getSort).orderByDesc(ReceiptUser::getId);
        List<ReceiptUser> ReceiptUserList = dao.selectList(lqw);

        List<ReceiptUser> responseList = new ArrayList<>();
        if (CollUtil.isEmpty(ReceiptUserList)) {
            return CommonPage.copyPageInfo(ReceiptUserPage, responseList);
        }
        for (ReceiptUser ReceiptUser : ReceiptUserList) {
            ReceiptUser response = new ReceiptUser();
            BeanUtils.copyProperties(ReceiptUser, response);
            responseList.add(response);
        }
        return CommonPage.copyPageInfo(ReceiptUserPage, responseList);
    }


    /**
     * 查询详情
     *
     * @param id Integer
     * @return ReceiptUserVo
     */
    @Override
    public ReceiptUser getVoByFront(Integer id)  {
        ReceiptUser pmsReceiptUser = getById(id);
//        if (ObjectUtil.isNull(pmsReceiptUser) || pmsReceiptUser.getIsDel()==1) {
//            throw new CommonException("不存在");
//        }
//        if (pmsReceiptUser.getStatus()==0) {
//            throw new CommonException("不存在");
//        }
        ReceiptUser ReceiptUserResponse = new ReceiptUser();
        BeanUtils.copyProperties(pmsReceiptUser, ReceiptUserResponse);
//        if (addReceiptUserVisit(id)) {
//            logger.error("增加阅读次数失败，id = {}", id);
//        }
        return ReceiptUserResponse;
    }

    /**
     * 获取详情
     *
     * @param id id
     * @return ReceiptUser
     */
    @Override
    public ReceiptUser getDetail(Integer id) {
        ReceiptUser ReceiptUser = getByIdException(id);
        ReceiptUser response = new ReceiptUser();
        BeanUtils.copyProperties(ReceiptUser, response);
        return response;
    }



    /**
     * 增加浏览次数
     *
     * @param id id
     */
    private Boolean addReceiptUserVisit(Integer id) {
        UpdateWrapper<ReceiptUser> wrapper = Wrappers.update();
        wrapper.setSql("visit = visit + 1");
        wrapper.eq("id", id);
        return update(wrapper);
    }


    /**
     * 获取移动端热门列表
     *
     * @return List<ReceiptUser>
     */
    @Override
    public List<ReceiptUser> getHotList() {
        LambdaQueryWrapper<ReceiptUser> lqw = Wrappers.lambdaQuery();
//        lqw.select(ReceiptUser::getId, ReceiptUser::getCover, ReceiptUser::getTitle, ReceiptUser::getCreateTime);
//        lqw.eq(ReceiptUser::getIsHot, true);
//        lqw.eq(ReceiptUser::getStatus, true);
//        lqw.eq(ReceiptUser::getIsDel, false);
//        lqw.orderByDesc(ReceiptUser::getSort, ReceiptUser::getId);
        lqw.last(" limit 20");
        List<ReceiptUser> ReceiptUserList = dao.selectList(lqw);
        if (CollUtil.isEmpty(ReceiptUserList)) {
            return CollUtil.newArrayList();
        }
        return ReceiptUserList.stream().map(e -> {
            ReceiptUser ReceiptUserResponse = new ReceiptUser();
            BeanUtils.copyProperties(e, ReceiptUserResponse);
            return ReceiptUserResponse;
        }).collect(Collectors.toList());
    }

    /**
     * 新增
     *
     * @param request 新增参数
     * @return Boolean
     */
    @Override
    public Boolean create(ReceiptUser request) {
        ReceiptUser ReceiptUser = new ReceiptUser();
        BeanUtils.copyProperties(request, ReceiptUser);
//        ReceiptUser.setId(null);
////        String cdnUrl = systemAttachmentService.getCdnUrl();
////        ReceiptUser.setCover(systemAttachmentService.clearPrefix(ReceiptUser.getCover(), cdnUrl));
////        ReceiptUser.setContent(systemAttachmentService.clearPrefix(ReceiptUser.getContent(), cdnUrl));
//        ReceiptUser.setVisit(0L);
        return save(ReceiptUser);
    }


    /**
     * 修改
     *
     * @param request 修改参数
     */
    @Override
    public Boolean updateReceiptUser(ReceiptUser request) {
        if (ObjectUtil.isNull(request.getId())) {
            throw new CommonException("ID不能为空");
        }
        ReceiptUser  ReceiptUser  =  getByIdException(request.getId());
//        if(ReceiptUser.getMemberId().equals(request.getMemberId())){

//        ReceiptUser ReceiptUser = new ReceiptUser();
            BeanUtils.copyProperties(request, ReceiptUser);
//        String cdnUrl = systemAttachmentService.getCdnUrl();
//        ReceiptUser.setCover(systemAttachmentService.clearPrefix(ReceiptUser.getCover(), cdnUrl));
//        ReceiptUser.setContent(systemAttachmentService.clearPrefix(ReceiptUser.getContent(), cdnUrl));
            return updateById(ReceiptUser);
//        }else{
//            throw new CommonException("操作错误");
//        }
    }




    /**
     * 修改
     *
     * @param request 修改参数
     */
    @Override
    public Boolean pushReceiptUser(ReceiptUser request) {
//        if (ObjectUtil.isNull(request.getId())) {
//            throw new CommonException("ID不能为空");
//        }
//        getByIdException(request.getId());
//        ReceiptUser ReceiptUser = new ReceiptUser();
//        BeanUtils.copyProperties(request, ReceiptUser);


//        String[] imagesdList = request.getImages().split(CommonConstant.DH_REGEX);
//
//        if(imagesdList!=null) request.setCover(imagesdList[0]);

        if(request.getId() == null){
            return  create(request);
        }else{
            return updateReceiptUser(request);
        }


    }



    /**
     * 删除
     *
     * @param id id
     * @return Boolean
     */
    @Override
    public Boolean deleteById(Integer id) {
        ReceiptUser ReceiptUser = getByIdException(id);
        ReceiptUser.setIsDel(1);
        return updateById(ReceiptUser);
    }


    @Override
    public Boolean deleteByIdWithMemberId(Integer id,Integer memberId) {
        ReceiptUser receiptUser = getByIdMemberIdException(id,memberId);

        System.out.println(id);
        System.out.println(memberId);


        if (memberId.equals(0) ) {
            throw new CommonException("操作错误1！！！！！！");
        }

        if (!memberId.equals(receiptUser.getUid())) {
            throw new CommonException("操作错误2！！！！！！");
        }



        receiptUser.setIsDel(1);
        return updateById(receiptUser);
    }

    private ReceiptUser getByIdException(Integer id) {
        ReceiptUser ReceiptUser = getById(id);
        if (ObjectUtil.isNull(ReceiptUser) || ReceiptUser.getIsDel()==1) {
            throw new CommonException("不存在");
        }
        return ReceiptUser;
    }

    private ReceiptUser getByIdMemberIdException(Integer id,Integer memberId) {
        ReceiptUser ReceiptUser = getById(id);

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("uid", memberId);
        List<ReceiptUser> list = dao.selectByMap(map);

        list.forEach(System.out::println);



        if (ObjectUtil.isNull(ReceiptUser) || ReceiptUser.getIsDel()==1) {
            throw new CommonException("不存在");
        }
        return ReceiptUser;
    }



    /**
     * 是否使用分类
     *
     * @param categoryId 分类ID
     * @return Boolean
     */
    @Override
    public Boolean isUseCategory(Integer categoryId) {
        LambdaQueryWrapper<ReceiptUser> lqw = Wrappers.lambdaQuery();
//        lqw.eq(ReceiptUser::getType, categoryId);
        lqw.eq(ReceiptUser::getIsDel, false);
        lqw.eq(ReceiptUser::getIsDel, false);
        lqw.last(" limit 1");
        ReceiptUser ReceiptUser = dao.selectOne(lqw);
        return ObjectUtil.isNotNull(ReceiptUser);
    }



    /**
     * 开关
     * @param id id
     * @return Boolean
     */
    @Override
    public Boolean ReceiptUserSwitch(Integer id) {
        ReceiptUser ReceiptUser = getByIdException(id);
        ReceiptUser.setIsDel(ReceiptUser.getIsDel()==1?0:1);
        return updateById(ReceiptUser);
    }
}
